package ewms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import ewms.bo.EwmsTransferWarehouseDetailAddBo;
import ewms.bo.EwmsTransferWarehouseDetailEditBo;
import ewms.bo.EwmsTransferWarehouseDetailQueryBo;
import ewms.common.utils.PageUtils;
import ewms.common.core.page.PagePlus;
import ewms.common.core.page.TableDataInfo;
import ewms.service.IEwmsTransferWarehouseDetailService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import ewms.domain.EwmsTransferWarehouseDetail;
import ewms.mapper.EwmsTransferWarehouseDetailMapper;
import ewms.vo.transfer_warehouse.EwmsTransferWarehouseDetailVo;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 危废转库明细Service业务层处理
 *
 * @author zhaoxiaochun
 * @date 2022-02-15
 */
@Service
public class EwmsTransferWarehouseDetailServiceImpl extends ServiceImpl<EwmsTransferWarehouseDetailMapper, EwmsTransferWarehouseDetail> implements IEwmsTransferWarehouseDetailService {

    @Override
    public EwmsTransferWarehouseDetailVo queryById(Long transferWarehouseDetailId){
        return getVoById(transferWarehouseDetailId, EwmsTransferWarehouseDetailVo.class);
    }

    @Override
    public TableDataInfo<EwmsTransferWarehouseDetailVo> queryPageList(EwmsTransferWarehouseDetailQueryBo bo) {
        PagePlus<EwmsTransferWarehouseDetail, EwmsTransferWarehouseDetailVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), EwmsTransferWarehouseDetailVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<EwmsTransferWarehouseDetailVo> queryList(EwmsTransferWarehouseDetailQueryBo bo) {
        return listVo(buildQueryWrapper(bo), EwmsTransferWarehouseDetailVo.class);
    }

    private LambdaQueryWrapper<EwmsTransferWarehouseDetail> buildQueryWrapper(EwmsTransferWarehouseDetailQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EwmsTransferWarehouseDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTransferWarehouseId() != null, EwmsTransferWarehouseDetail::getTransferWarehouseId, bo.getTransferWarehouseId());
        lqw.eq(bo.getWasteInventoryId() != null, EwmsTransferWarehouseDetail::getWasteInventoryId, bo.getWasteInventoryId());
        lqw.eq(bo.getFromWarehouseId() != null, EwmsTransferWarehouseDetail::getFromWarehouseId, bo.getFromWarehouseId());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(EwmsTransferWarehouseDetailAddBo bo) {
        EwmsTransferWarehouseDetail add = BeanUtil.toBean(bo, EwmsTransferWarehouseDetail.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(EwmsTransferWarehouseDetailEditBo bo) {
        EwmsTransferWarehouseDetail update = BeanUtil.toBean(bo, EwmsTransferWarehouseDetail.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(EwmsTransferWarehouseDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
